package net.net23.sysdent.server.services;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import net.net23.sysdent.client.services.model.MapService;
import net.net23.sysdent.shared.MapServiceContants;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class MapServiceImp extends RemoteServiceServlet implements MapService {

	Logger log = Logger.getLogger(MapServiceImp.class.getName());

	@Override
	public Map<Object, Object> execute(byte action, Map<Object, Object> params) {
		switch (action) {
		case MapServiceContants.GET_USER_INFO:
			return getUserName(params);
		case MapServiceContants.GET_LOGOUT_URL:
			return getLogoutUrl(params);
		default:
			return null;
		}
	}

	private Map<Object, Object> getUserName(Map<Object, Object> params) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		Map<Object, Object> userInfo = new HashMap<Object, Object>();
		userInfo.put(MapServiceContants.GET_USER_INFO_AUTH_DOMAIN,
				user.getAuthDomain());
		userInfo.put(MapServiceContants.GET_USER_INFO_EMAIL, user.getEmail());
		userInfo.put(MapServiceContants.GET_USER_INFO_FEDERATED_IDENTITY,
				user.getFederatedIdentity());
		userInfo.put(MapServiceContants.GET_USER_INFO_NICK_NAME,
				user.getNickname());
		userInfo.put(MapServiceContants.GET_USER_INFO_ID, user.getUserId());
		userInfo.put(MapServiceContants.GET_LOGOUT_URL_URL,
				userService.createLogoutURL(""));
		return userInfo;
	}

	private Map<Object, Object> getLogoutUrl(Map<Object, Object> params) {
		UserService userService = UserServiceFactory.getUserService();
		Map<Object, Object> logoutUrl = new HashMap<Object, Object>();
		logoutUrl.put(MapServiceContants.GET_LOGOUT_URL_URL,
				userService.createLogoutURL(""));
		return logoutUrl;
	}

}
